package com.chinatelecom.clientservice.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chinatelecom.clientservice.pojo.BaseRoles;
import com.chinatelecom.clientservice.pojo.BaseUsers;
import com.chinatelecom.clientservice.pojo.ExtGridReturn;
import com.chinatelecom.clientservice.pojo.ExtPager;
import com.chinatelecom.clientservice.pojo.ExtReturn;
import com.chinatelecom.clientservice.service.UserService;

@Controller
@RequestMapping(value = "/user")
public class UserController {
	@Autowired
	private UserService userService;

	@RequestMapping(method = RequestMethod.GET)
	public String user() {
		return "user/user";
	}

	@RequestMapping(value = "/all")
	@ResponseBody
	public ExtGridReturn all(ExtPager pager, @RequestParam(required = false, defaultValue = "") String realName) {
		ExtGridReturn extGridReturn = userService.selectUserInfo(pager, realName);
		return extGridReturn;
		// Gson gson = new Gson();
		// return gson.toJson(extGridReturn);
	}

	/**
	 * 查询所有角色信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/roleList")
	@ResponseBody
	public Map<String, List<BaseRoles>> roleList() {
		List<BaseRoles> roleList = userService.selectRoleList();
		Map<String, List<BaseRoles>> map = new HashMap<String, List<BaseRoles>>();
		if (roleList != null && !roleList.isEmpty()) {
			map.put("roleList", roleList);
		} else {
			map.put("roleList", null);
		}
		return map;
	}

	/**
	 * 保存用户
	 * 
	 * @param user
	 * @param roleIds
	 * @return
	 */
	@RequestMapping(value = "/saveUser", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public ExtReturn saveUserInfo(BaseUsers user, @RequestParam(value = "roleIds") List<String> roleIds) {
		int result = userService.saveUserInfo(user, roleIds);
		if (result == -1) {
			return new ExtReturn(false, "账号已经被注册，请重新填写!");

		} else if (result == 1) {
			return new ExtReturn(false, "出现异常");
		} else if (result == 0) {
			return new ExtReturn(true, "保存成功!");
		}
		return new ExtReturn(false, "出现异常!");
	}

	@RequestMapping(value = "/role/{userId}")
	@ResponseBody
	public Object roleInfoByUserId(@PathVariable String userId) {
		try {
			Map results = new HashMap();
			List<BaseRoles> roleList = userService.selectUserRoleInfo(userId);
			results.put("success", true);
			results.put("data", roleList);
			return results;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping(value = "/user/{userId}")
	@ResponseBody
	public Object userInfoByUserId(@PathVariable String userId) {
		try {
			Map results = new HashMap();
			results.put("success", true);
			results.put("data", userService.selectUserInfoByUserId(userId));
			return results;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
